package ac.github.os.internal.core.script.impl

import ac.github.os.internal.core.script.BaseWrapper
import ac.github.os.internal.core.script.InternalConfig
import ac.github.os.internal.core.script.InternalScript
import ac.github.os.internal.core.trigger.Trigger
import taboolib.common.platform.Awake
import taboolib.common.util.random

@Awake
class RandomScript : InternalScript<RandomScript.Wrapper> {

    class Wrapper : BaseWrapper

    val units = mutableMapOf<String, RandomUnit>()

    interface RandomUnit {
        fun execute(trigger: Trigger?, wrapper: Wrapper, string: String): String?
    }

    init {
        units["double"] = DoubleUnit()
        units["int"] = IntUnit()
    }

    override val name: String
        get() = "random"

    override fun execute(trigger: Trigger?, wrapper: Wrapper, config: InternalConfig, string: String): String? {
        val split = string.split(":")
        val target = split[0]
        val substring = string.substring(target.length + 1)
        return units[target]?.execute(trigger, wrapper, substring)
    }

    class IntUnit : RandomUnit {
        override fun execute(trigger: Trigger?, wrapper: Wrapper, string: String): String? {
            val split = string.split("-")
            val min = split[0].toInt()
            val max = if (split.size == 2) split[1].toInt() else min
            return (min..max).random().toString()
        }
    }

    class DoubleUnit : RandomUnit {
        override fun execute(trigger: Trigger?, wrapper: Wrapper, string: String): String? {
            val split = string.split("-")
            val min = split[0].toDouble()
            val max = if (split.size == 2) split[1].toDouble() else min
            return String.format("%.2f",random(min, max))
        }
    }



}